#import "FoursquareVenue.h"

//  This Foursquare integration is possible thanks to the Foursquare API by
//  Eric Butler and associates.
//
//  Copyright (C) 2010 Eric Butler <eric@codebutler.com>

/**
 * A FoursquareVenue is an object representing a single venue in the
 * Foursquare system.
 *
 * Code used within this class and the supporting API was written by 
 * Eric Butler and other associates.
 *
 * The supporting API can be found in ./FourSquareAPI folder.
 *
 * @author      Eric Butler
 * @author      James Lin
 * @version     2.2.1 
 * @since       2.0.5
 */
@implementation FoursquareVenue

@synthesize venueID;       // Identifier of venue
@synthesize venueName;     // Name of venue
@synthesize venueAddress;  // Address of venue
@synthesize venueCity;     // City of venue
@synthesize venueDistance; // Distance to venue
@synthesize venueCategory; // Category of venue
@synthesize venueImageURL; // URL of venue image
@synthesize venueLocation; // Location of venue
@synthesize venueCheckIns; // Check-ins at venue
@synthesize venueTips;     // Tips for venue
@synthesize venueSpecials; // Specials for venue

/**
 * Initialize the Foursquare venue.
 *
 * @param identifier  the identifier for the venue
 * @param name        the name of venue
 * @param address     the address of venue
 * @param city        the city of venue
 * @param distance    the distance to the venue
 * @param category    the category of venue
 * @param latitude    the latitude of venue location
 * @param longitude   the longitude of the venue location
 * @param specials    the specials at the venue
 * @return            the initialized Foursquare venue
 */
- (id)initWithID:(NSString*)identifier 
			name:(NSString*)name 
		 address:(NSString*)address 
			city:(NSString*)city 
		distance:(NSString*)distance 
		category:(NSString*)category 
		latitude:(NSString*)latitude 
	   longitude:(NSString*)longitude 
		specials:(NSDictionary*)specials {
	
	if (self = [super init]) {
		self.venueID = identifier;
		self.venueName = name;
		self.venueAddress = address ? address : @"";
		self.venueCity = city ? city : @"";
		self.venueDistance = distance ? distance : @"";
		self.venueCategory = category ? category : @"";
		venueLocation.latitude = [latitude floatValue]; 
		venueLocation.longitude = [longitude floatValue];
		self.venueSpecials = specials ? specials : nil;
		self.venueImageURL = @"";
	}
	
	return self;
}

/**
 * Load the additional venue information: check-ins, tips, and specials.
 */
- (void)loadExtraInfo {
	if (!venueID) {
		return;
	}
	
	[Foursquare detailForVenue:self.venueID callback:
	 ^(BOOL success, id response) {
		 if (success) {
			 // Store the check-in and tips data
			 NSDictionary* venueDict = [response objectForKey:@"venue"];
			 NSDictionary* statsDict  = [venueDict objectForKey:@"stats"];
			 
			 self.venueCheckIns = [[statsDict objectForKey:@"checkins"] intValue];
			 self.venueTips = [venueDict objectForKey:@"tips"];
		 } else {
			 // Callback failed
			 self.venueCheckIns = -1;
		 }
	 }];
}

/**
 * Deallocate the Foursquare venue.
 */
- (void)dealloc {
	[venueID release], venueID = nil;
	[venueName release], venueName = nil;
	[venueAddress release], venueAddress = nil;
	[venueCity release], venueCity = nil;
	[venueDistance release], venueDistance = nil;
	[venueCategory release], venueCategory = nil;
	[venueImageURL release], venueImageURL = nil;
	[venueTips release], venueTips = nil;
	[venueSpecials release], venueSpecials = nil;
	[super dealloc];
}

@end
